#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Base{
public:
    Base(const char * pbase)
    : _pbase(new char[strlen(pbase) + 1]())
    {
        strcpy(_pbase,pbase);
    }

    Base(const Base & rhs)
    : _pbase(new char[strlen(rhs._pbase) + 1]())
    {
        strcpy(_pbase,rhs._pbase);
    }

    Base & operator=(const Base & rhs)
    {
        if(this != &rhs){
            delete [] _pbase;
            _pbase = new char[strlen(rhs._pbase) + 1]();
            strcpy(_pbase,rhs._pbase);
        }
        return *this;
    }

    void print() const{
        cout << _pbase << endl;
    }

    ~Base(){
        if(_pbase){
            delete [] _pbase;
            _pbase = nullptr;
        }
    }

protected:
    char * _pbase;
};

class Derived
: public Base
{
public:
    Derived(const char * pbase)
    : Base(pbase)
    {}

    void display() const{
        print();
        cout << _derived << endl;
    }

protected:
    long _derived = 10;
};

void test0(){
    Derived d1("hello");
    d1.display();
    cout << "执行拷贝构造：" << endl;
    Derived d2 = d1;
    d2.display();
    cout << "赋值运算符函数" << endl;
    Derived d3("world!!!");
    d2 = d3;
    d2.display();
}

int main(void){
    test0();
    return 0;
}
